Page({

  /**
   * 页面的初始数据
   */
  data: {
    classify:[],
    curClassify:0,
    doneList:[],
    undoneList:[],
    done_num:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  addTodo:function () {
    wx.navigateTo({
      url: '/pages/addTodo/addTodo',
    })
  },

  intoToDo:function (e) {
    wx.navigateTo({
      url: '/pages/todoDetail/todoDetail?detail='+JSON.stringify(e.currentTarget.dataset.detail),
    })
  },

  setDone:function(e){
    var that=this
    wx.cloud.callFunction({
      name: 'updateDone',
      data:{
        id:e.currentTarget.dataset.undoneset._id,
        done:true
      },
      success:function (res) {
        that.onShow()
      }
      })
  },

  setUndone:function(e){
    var that=this
    wx.cloud.callFunction({
      name: 'updateDone',
      data:{
        id:e.currentTarget.dataset.doneset._id,
        done:false
      },
      success:function (res) {
        that.onShow()
      }
      })
  },

  renameClassification:function (e) {
    var that=this
    let rename=e.detail.value
    wx.cloud.callFunction({
        name: 'msgCheck',
        data:{
          content:rename
        },
        success:function (res1) {
            if(res1.result.result.suggest==="pass"){
              wx.cloud.callFunction({
                name: 'renameClassification',
                data:{
                  classify:that.data.classify[that.data.curClassify],
                  renamedClassify:rename
                },
                success:function (res) {
                  that.onShow()
                }
                })
            }
            else{
              //console.log(res1.result.result.suggest)
              wx.showModal({
                title: '提示',
                content: '当前输入包含违规内容，请重新输入后再试',
                success: function(res) {
                    that.onShow()
                }
            })
        }
      }
    })
  },
    
    

  deleteClassification:function () {
    var that=this
    wx.showModal({
      title: '提示',
      content: '确定删除当前分类?',
      success: function(res) {
        if (res.confirm) {
          wx.cloud.callFunction({
            name: 'deleteClassification',
            data:{
              classify:that.data.classify[that.data.curClassify]
           },
           success:function (res) {
            that.onShow()
          }
          })
        } 
      }
    })
  },

  selectClassify:function(e){
    this.setData({
      curClassify:e.detail.value
  })
    var that=this
    wx.cloud.callFunction({
      name: 'getToDo',
      data:{
        classify:that.data.classify[that.data.curClassify],
        needHasDone:1
     },
      success:function (res) {
      that.setData({
        doneList:res.result.data,
        done_num:res.result.data.length
      })
      }
    })
    wx.cloud.callFunction({
      name: 'getToDo',
      data:{
        classify:that.data.classify[that.data.curClassify],
        needHasDone:-1
     },
      success:function (res1) {
      that.setData({
        undoneList:res1.result.data
      })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this
    wx.cloud.callFunction({
      name: 'getClassification',
      success:function (res) {
        that.setData({
          classify:res.result,
        }),
        wx.cloud.callFunction({
          name: 'getToDo',
          data:{
            classify:res.result[that.data.curClassify],
            needHasDone:1
          },
          success:function (res1) {
            that.setData({
              doneList:res1.result.data,
              done_num:res1.result.data.length
            })
            wx.cloud.callFunction({
              name: 'getToDo',
              data:{
                classify:res.result[that.data.curClassify],
                needHasDone:-1
              },
              success:function (res2) {
                that.setData({
                  undoneList:res2.result.data
                })
            }
          })
        }
      })

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    wx.stopPullDownRefresh({
      success: (res) => {},
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:'',
      path:'/pages/index/index',
      imageUrl:'/images/icon.png',
      success:(res)=>{
        
      }
  }
  }
})